// Developer: Robert Hede, Denmark
// As a part of giving open source code, I will ask you to leave this original information 
// and extend with own history of changes.
// +45 40 42 95 05
// DONG Energy 

using System;
using System.Collections.Generic;
using System.Text;

// required namespace for MOSS Dev.
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.ComponentModel;

using GMapControl;

namespace MapCtrlMOSS
{

    public class MapCtrlMOSS : WebPart
    {


        protected string _MapLicense;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            Category("Google"),
           WebDisplayName("Google MapLicense"),
           WebDescription("Enter license for Google Maps")]
        public string MapLicense
        {
            get { return _MapLicense;  }
            set { _MapLicense = value; }
        }


        protected bool _ShowEvents;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            Category("MapCtrlMOSS"),
          WebDisplayName("Show Events"),
           WebDescription("Enable og disable event information e.g. coordinates of mouseclick.")]
        public bool ShowEvents
        {
            get { return _ShowEvents; }
            set { _ShowEvents = value; }
        }

        protected bool _EventsActivated;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            Category("MapCtrlMOSS"),
          WebDisplayName("React on events in code-behind"),
          WebDescription("Enable or disable onmouseclick events, for returning coordinates in asp.net code-behind.")]
        public bool EventsActivated
        {
            get { return _EventsActivated; }
            set { _EventsActivated = value; }
        }


        protected double _longtitude;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            Category("MapCtrlMOSS"),
        WebDisplayName("Long. "),
           WebDescription("Map extent information")]
        public double longtitude
        {
            get { return _longtitude; }
            set { _longtitude = value; }
        }

        protected double _latitude;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            Category("MapCtrlMOSS"),
       WebDisplayName("Lat."),
           WebDescription("Map extent information")]
        public double latitude
        {
            get { return _latitude; }
            set { _latitude = value; }
        }

        protected int _zoomLevel;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            Category("MapCtrlMOSS"),
       WebDisplayName("Zoom level"),
           WebDescription("Map extent information")]
        public int zoomLevel
        {
            get { return _zoomLevel; }
            set { _zoomLevel = value; }
        }


        public enum MapTypeEnum
        {
            G_NORMAL_MAP = 0,
            G_SATELLITE_MAP=1,
            G_HYBRID_MAP=2,
            G_DEFAULT_MAP_TYPES=3
        };
        protected GMapControl.GMapControl.MapTypeEnum _mapThemeType;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
       DefaultValue(MapTypeEnum.G_HYBRID_MAP),
            Category("MapCtrlMOSS"),
       WebDisplayName("Map type"),
           WebDescription("Choose theme of the map")]
        public GMapControl.GMapControl.MapTypeEnum mapThemeType
        {
            get
            {
                return _mapThemeType;
            }
            set
            {
                _mapThemeType = value;
            }
        }


        protected string _DrawLayer;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            Category("MapCtrlMOSS"),
           WebDisplayName("Draw layer File"),
          WebDescription("Location of the XML content with drawing description.\nFormat <markers>\n  <marker lat=\"55.536523929284385\" lng=\"9.616073369979858\" info=\"Sample pushpin\" />\n</markers>\nOptional put in marker info/minipage and CDATA HTML")]
        public string DrawLayer
        {
            get { return _DrawLayer; }
            set { _DrawLayer = value; }
        }

        protected string _mapName;
        [Personalizable(PersonalizationScope.Shared),
            WebBrowsable(true),
            Category("MapCtrlMOSS"),
          WebDisplayName("MapName"),
         WebDescription("Name of the DIV in which the map is to be placed. The name is prefixed with large-google-map.\nIf more than one mapcontrol is placed on a page, then use unique names")]
        public string mapName
        {
            get { return _mapName; }
            set { _mapName = value; }
        }

        protected override void CreateChildControls()

        {
            // add map
            GMapControl.GMapControl gM = new GMapControl.GMapControl(Convert.ToInt32(Width.Value), Convert.ToInt32(Height.Value));
            gM.MessageDisplay = _ShowEvents;
            gM.StandardTools = true;
            gM.ZoomTool = true;
            gM.GoogleMapLicense = _MapLicense;
            gM.MapType=true;
            gM.OverviewMap=true;
            gM.WidthMap = Convert.ToInt32(Width.Value)-80;
            gM.HeightMap= Convert.ToInt32( Height.Value)-80;
            Width= Convert.ToInt32(Width.Value);
            Height=Convert.ToInt32(Height.Value);
            gM.Enabled = true;

            gM.longtitude = _longtitude;
            gM.latitude = _latitude;   // DK 55.49441555474223, 9.604110717773437), 6
            gM.zoomLevel = _zoomLevel;
            gM.eventsActivated= _EventsActivated;
            gM.DrawLayer = _DrawLayer;
            gM.mapThemeType = _mapThemeType;
            gM.mapName = _mapName;
            Controls.Add(gM);

        }



        public override void RenderControl(HtmlTextWriter writer)
        {
            string str = "";
            

            writer.Write(str);
            RenderChildren(writer);
            ClientScriptManager csm = Page.ClientScript;
            csm.RegisterStartupScript( this.GetType(), "refreshMap", "<script>map.checkResize(); </script>");
            
        }


        
    }

}
